&lt;?php
/**
 * CRMLite <?php echo $this->controller; ?> Controller
 *
 * @final
 * @package     Crmlite_App
 * @version     $Id: controller.php 39 2007-09-02 12:49:05Z etng2004 $
 * @copyright   2006-2007 ETE.CN
 * @author      Yi BO  <etng2004@gmail.com>
 * @license     PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
require_once dirname(__FILE__) . '/AdminBaseController.php';
final class <?php echo $this->module=='default'?'':(ucfirst($this->module) . '_'); ?><?php echo $this->controller; ?>Controller extends AdminBaseController
{
	/**
	 * 默认的模型
	 *
	 * 未初始化时是字符串，初始化之后就是一个模型接口
	 *
	 * @var Crmlite_Model
	 */
	protected $model = 'ContactHistory';
	protected $scaffholding = false;
	public function indexAction()
	{
		$this->_forward('list');
	}

	/**
	 * @todo 处理相应条件进行处理
	 *
	 */
	public function listAction()
	{
		$where = array();
		/**
		 * @todo 处理筛选条件并设置排序方式
		 */
		$order = null;
		/**
		 * @todo 处理筛选字段 并设置默认值
		 */
/*		$default_statics = array();
		$this->view->statics = $this->model->statics($where, 'status', $default_statics);*/

    	$pager = array(
				'curpage' 	=> $this->_getParam('page', 	1),
				'perpage'	=> $this->_getParam('perpage', 10),
				'total'		=> $this->model->count($where),
				'url'		=> $this->base_url . '/list',
				);
		$this->view->pager = $pager;
		$this->view-><?php echo $this->plural_object ; ?> = $this->model->paginate($where, $order, $pager['curpage'], $pager['perpage']);
		$this->setTitle('所有', $pager['total']);
	}
	/**
	 * @todo 设置相应默认字段
	 *
	 */
	public function addAction()
	{
		if($this->isPost())
    	{
    		$record = $_POST['fields']['<?php echo $this->single_object ; ?>'];
    		$record['report_at'] = REQUEST_TIME;
    		if($this->model->insert($record))
    		{
    			$this->removeCache($record['id']);
    			$this->log($msg = "<?php echo $this->class_object ; ?> #{$record['id']} created!");
    			$this->feedback($msg, '成功', $this->base_url);
    		}
    		$this->view-><?php echo $this->single_object ; ?> = $record;
    	}
    	/**
    	 * 初始化默认参数
    	 */
    	$this->setTitle('新增');
	}
	/**
	 * @todo 设置相应默认字段
	 *
	 */
    public function editAction()
    {
    	if($this->isPost())
    	{
    		$record = $_POST['fields']['<?php echo $this->single_object ; ?>'];
    		$id = $record['id'];
    		/**
    		 * 设置更新时的默认字段
    		 */
//    		$record['updated_at'] = REQUEST_TIME;
     		if($this->model->update($record, "id = {$id}"))
    		{
    			$this->removeCache($id);
    			$this->log($msg = "<?php echo $this->class_object ; ?> #{$record['id']} updated!");
    			$this->feedback($msg, '成功', $this->base_url);
    		}
    		$this->view-><?php echo $this->single_object ; ?> = $record;
    		$error = 'Update failed!';
    	}
    	/**
    	 * 若不是POST方法则为初始编辑
    	 */
    	$this->setTitle('修改');
    	if(empty($error))
    	{
			$id = intval($this->_getParam('id', 0));
			$this->view->client = $this->model->find($id);
    	}
    }
	/**
	 * @todo 设置相应限制
	 * @todo 出错处理
	 *
	 *
	 */
    public function viewAction()
    {
		if($id = intval($this->_getParam('id', 1)))
		{
			$this->view-><?php echo $this->single_object ; ?> = $<?php echo $this->single_object ; ?> = $this->model->find($id);
			$this->setTitle('查看#' . $<?php echo $this->single_object ; ?>['id']);
		}
		else
		{
			exit(__METHOD__);
		}
    }

   /**
     * 删除到回收站
     * @todo 出错处理
     *
     */
    public function trashAction()
    {
		if(	$id = intval($this->_getParam('id', 1)))
		{
			if($status = $this->model->trashById($id))
			{
				$this->removeCache($id);
				$this->log($msg = "<?php echo $this->class_object ; ?> #{$id} now is marked as deleted!");
				$this->feedback($msg, '成功', $this->base_url);
			}
		}
		exit(__METHOD__);
    }
    /**
     * 从回收站回复删除，应检查操作权限
     * @todo 出错处理
     * @todo 实现权限检查
     *
     */
    public function restoreAction()
    {
		if(	$id = intval($this->_getParam('id', 1)))
		{
			if($status = $this->model->restoreById($id))
			{
				$this->removeCache($id);
				$this->log($msg = "<?php echo $this->class_object ; ?> #{$id} now is marked as deleted!");
				$this->feedback($msg, '成功', $this->base_url);
			}
		}
		exit(__METHOD__);
    }
   /**
     * 彻底删除，应检查操作权限
     * @todo 出错处理
     * @todo 实现权限检查
     *
     */
    public function deleteAction()
    {
		if(	$id = intval($this->_getParam('id', 1)))
		{
			if($status = $this->model->deleteById($id))
			{
				$this->removeCache($id);
				$this->log($msg = "<?php echo $this->class_object ; ?> #{$id} now is marked as deleted!");
				$this->feedback($msg, '成功', $this->base_url);
			}
		}
		exit(__METHOD__);
    }
    /**
     * 批量操作，如设置状态，删除等等
     * @todo 实现主要的操作方式
     */
    public function batchOperateAction()
    {
    	/**
    	 * WE do not need render from template!
    	 */
    	$this->_helper->viewRenderer->setNoRender();
    	if($this->isPost())
    	{
    		var_dump($_POST);
    	}
    }
    /**
     * 搜索接口，将转向list
     *
     */
    public function searchAction()
    {
		$this->_forward('index');
    }
}
